<?php


namespace app\index\model;


use think\Exception;
use think\Loader;
use think\Model;
use think\Request;

class Vendor extends Model
{

/*    public function getVendorName()
    {
//        $vendorClass = new self();
        $data=$this->where('del_flag', 0)->field('company,address,telephone')->select()->toArray();
        return ['vendor_list'=>$data];
    }*/

    public static function getVendorCompanyNameList(){
        $VendorClass=new self();
        return $VendorClass->where('del_flag',0)->field('id value,company label,address,telephone')->select()->toArray();
    }

    public static function getVendorById($id){
        $VendorClass=new self();
        $vendor=$VendorClass->where('id',$id)->where('del_flag',0)->field('id,name,company,address,wechat,remark,telephone,web')->find();
        return $vendor;
    }

    /**
     * 获取供应商列表
     * create by yhb on 2021/3/23 16:47
     * @param $search_param
     * @param $page_param
     * @return array
     * @throws \think\exception\DbException
     */
    public function getVendorList($search_param,$page_param)
    {
        $vendorClass = new self();
        $where['del_flag']=['=',0];
        if (!empty($search_param['vendor_list'])) {
            $where['id'] = ['=',$search_param['vendor_list']];
        }

        $obj=$vendorClass
            ->where($where)
            ->field('id,company, name, address, wechat, telephone, web, remark')
            ->paginate($page_param['page_size'],false,['page'=>$page_param['page_num']]);

        if($obj->isEmpty()){
            return ['status'=>true,'result'=>['product'=>[],'total'=>0]];
        }
        $count=$obj->total();
        $data=$obj->items();;
        return ['status'=>true,'result'=>['vendor'=>$data,'total'=>$count]];
    }

    /**
     * 添加供应商
     * create by yhb on 2021/3/23 16:47
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addVendor($data)
    {
        $vendorClass = new self();

        $obj = $vendorClass
            ->where('company',$data['company'])
            ->where('del_flag',0)
            ->field('id')
            ->find();
        if(!empty($obj)){
            return ['status'=>false,'result'=>'公司名已经被使用!'];
        }


        $vendorClass->data($data);
        $result=$vendorClass->save();
        if($result===false){
            return ['status'=>false,'result'=>'添加失败'];
        }
        return ['status'=>true,'result'=>'添加成功'];
    }

}